﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Win104.Common
{
    public class ConvertHelper
    {
        /// <summary>
        /// 十进制转二进制
        /// </summary>
        /// <param name="ten"></param>
        /// <returns></returns>
        public static string TenToBinary(int ten)
        {
            return Convert.ToString(ten, 2).PadLeft(8,'0');
        }

        /// <summary>
        /// 二进制转10进制
        /// </summary>
        /// <param name="bin"></param>
        /// <returns></returns>
        public static int BinaryToTen(string bin)
        {
            return Convert.ToInt32(bin, 2);
        }

        /// <summary>
        /// byte转换为十六进制
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static string ByteToHex(byte b)
        {
            return b.ToString("X2");
        }

        /// <summary>
        /// 十六进制转十进制
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static int HexToTen(string hex)
        {
            return Convert.ToInt32(hex, 16);
        }

        /// <summary>
        /// 十六进制字符串转byte数组
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static byte[] HexToBytes(string hex)
        {
            hex = hex.Replace(" ", "");
            byte[] resultBytes = new byte[hex.Length / 2];
            for (int i = 0; i < resultBytes.Length; i++)
            {
                resultBytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
            }
            return resultBytes;
        }

        /// <summary>
        /// byte数组转十六进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string BytesToHex(byte[] bytes, int length)
        {
            string resultStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < length; i++)
                {
                    if (i == length - 1)
                    {
                        resultStr += bytes[i].ToString("X2");
                    }
                    else
                    {
                        resultStr += bytes[i].ToString("X2") + " ";
                    }
                }
            }
            return resultStr;
        }

        private static char[] HEX_CHAR_TABLE = {
        '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
    };

        public static string ToHexString(byte[] data)
        {
            if (data == null || data.Length == 0)
                return null;

            byte[] hex = new byte[data.Length * 2];
            int index = 0;
            foreach (byte b in data)
            {
                int v = b & 0xFF;
                hex[index++] = (byte)HEX_CHAR_TABLE[v >> 4];
                hex[index++] = (byte)HEX_CHAR_TABLE[v & 0xF];
            }
            return System.Text.Encoding.UTF8.GetString(hex);
        }

        /// <summary>
        /// 4字节16进制转float
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static float GetHexFloat(string hexStr)
        {
            uint num = uint.Parse(hexStr, System.Globalization.NumberStyles.AllowHexSpecifier);
            byte[] floatVals = BitConverter.GetBytes(num);
            float f = BitConverter.ToSingle(floatVals, 0);
            return f;
        }

    }
}
